#-------------------------------------------------------------------------------
# Name:        CAAUuidGen.py
# Purpose:  Generate and transform uuid to CATIA usable form
#
# Author:      rawat
#
# Created:     14-02-2010
# Copyright:   (c) rawat 2010
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

"""
   Utility classes for generating uuid and converting uuid of form
          12ddfdc0-dedb-11d3-b844-0008c73f25e8
          to
         { 0x12ddfdc0, 0xdedb, 0x11d3, { 0xb8, 0x44, 0x00, 0x08, 0xc7, 0x3f, 0x25, 0xe8} };

Usage:
 	python CAAUuidGen.py -genIID >> TestCase.py 
to generate IID in following form
	UuidIID="bff19fbe-98be-4320-898a-0b14a460e6b8"
	UuidCPP="{ 0xbff19fbe, 0x98be, 0x4320, { 0x89, 0x8a, 0x0b, 0x14, 0xa4, 0x60, 0xe6, 0xb8} }"
"""

import uuid
import os
import sys

class CAAUuid(object):
    """
    convert uuid of form
          12ddfdc0-dedb-11d3-b844-0008c73f25e8
    to
         { 0x12ddfdc0, 0xdedb, 0x11d3, { 0xb8, 0x44, 0x00, 0x08, 0xc7, 0x3f, 0x25, 0xe8} };
    """

    def __init__(self, type):
        self.uid = uuid.uuid4()
        self.strUid = str(self.uid)
        self.type = type
        #print self.Transform(strUid)

    def Transform(self, strUuid):
        """
        convert uuid of form
          12ddfdc0-dedb-11d3-b844-0008c73f25e8
        to
       { 0x12ddfdc0, 0xdedb, 0x11d3, { 0xb8, 0x44, 0x00, 0x08, 0xc7, 0x3f, 0x25, 0xe8} };
        """

        uidFields = strUuid.split("-")
        #print uidFields

        strNull="0x"
        #1. add ox to first three
        newUid = "{ 0x" + uidFields[0] + ", 0x" + uidFields[1] + ", 0x" + uidFields[2] + ", {"

        #Make one string out of last two fields
        strLast=uidFields[3]  + uidFields[4]

        #add ox to every two characters
        nLength=len(strLast)
        for i in range(0, nLength, 2) :

            newUid += " 0x" + strLast[i : i+2]

            # add comma except for last
            if( i != nLength - 2):
                newUid += ","

        # closing brackets
        newUid += "} }"

        return newUid

    def GetTransformed(self):
        """ doc """

        # IID for IDL files have only three dashes - remove the last dash.
        if( self.type.startswith("UuidIDL")):
            uidFields = self.strUid.split("-")
            strUidForIDL = uidFields[0] + "-" + uidFields[1] + "-" + uidFields[2] + "-" + uidFields[3]  + uidFields[4]
            return strUidForIDL

        return self.Transform(self.strUid)

    def GetCWD(self):
        """ Returns the current working directory in windows format """

        if( os.name == "nt") :
            return os.getcwd()

        # The path is returned in cygwin style. os.name == posix
        # /cygdrive/e/UNIX/codepattern/ = ['', 'cygdrive', 'e', 'UNIX', 'codepattern', ]
        cwdArray = os.getcwd().split("/")
        cwd = cwdArray[2].upper() + ":"
        for i in range(3, len(cwdArray)) :
            cwd += "\\\\" + cwdArray[i]
            #print cwdArray[i]

        return cwd

def main():

    uid = CAAUuid("Uuid")
    print uid.GetTransformed(), uid.strUid

    uid = CAAUuid("UuidIDL")
    print uid.GetTransformed(), uid.strUid

    print "Current working directory ", uid.GetCWD()

def GenIID():
	fnames = ['Uuid', 'UuidFactory', 'UuidIDL', 'UuidIDLAlias', 'UuidIDLFactory','UuidIDLFactoryAlias', 'UuidIDLTypeLib', 'UuidDlgContextualMenu', 'UuidDlgFactory', 'UuidDlgList',  'UuidEntity', 'UuidEntityFilter', 'UuidEntityList', ]
	for fn in fnames:
		caauid = CAAUuid(fn)    
		newStr = caauid.GetTransformed()
		newStr = "\"" + newStr + "\"" 
		strUid = "\"" + caauid.strUid + "\"" 
		print "%sIID=%s" % (fn, strUid)
		if(fn.startswith("UuidIDL")):
			print "%sIDL=%s" % (fn, newStr)
		else:
			print "%sCPP=%s" % (fn, newStr)

	workingDir = CAAUuid("CurrentWorkingDir").GetCWD()
	#Adding single or double quotes runs into problem with \U e.g in \UNIX directory name
	workingDir = "\"" + workingDir + "\""  
	print "WorkingDir=%s" % (workingDir)
	
if __name__ == '__main__':
	if(len(sys.argv) > 1) :
		if( sys.argv[1] == "-genIID" ) :
			GenIID()
	else :
		main()

